/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.murillo.chufa.entry;

import org.murillo.chufa.entry.CacheEntry;
import java.lang.ref.ReferenceQueue;

/**
 *
 * @author Elisa
 */
public class SoftValueWeakKeyCacheEntry<K, V> extends CacheEntry<K, V> {

    private volatile WeakKey<K> key;

    public SoftValueWeakKeyCacheEntry(K key, V referent, ReferenceQueue queue, ReferenceQueue keyqueue, long creationCost, long tickStamp) {
        super(referent, queue, creationCost, tickStamp);
        this.key = new WeakKey<K>(key, keyqueue, this);
    }

    public SoftValueWeakKeyCacheEntry(K key, V referent, ReferenceQueue queue, long creationCost, SoftValueWeakKeyCacheEntry old) {
        super(referent, queue, creationCost, old.tickStamp);
        this.hits = old.hits;
        this.key = old.key;
        this.key.setParentEntry(this);
        this.one_costxhits = 1.0F / (creationCost * hits + 1);
    }
    
    @Override
    public K getKey() {
        return key.get();
    }
    
    public WeakKey<K> getWeakKey() {
        return key;
    }

    @Override
    public void dispose() {
        this.key = null;
        this.hardValue = null;
        //Ensure eviction in next cicle
        this.creationCost = 0;
        this.one_costxhits = 1;
        this.tickStamp = Long.MIN_VALUE;
    }
   
}
